package backend.category.controller;

import java.util.List;
import java.util.Map;
import java.util.Set;

import backend.category.manager.CategoryManager;
import backend.cms.manager.CmsManager;
import backend.paging.Paging;


import com.opensymphony.xwork2.ActionSupport;

import core.category.helper.CategoryPath;
import core.category.model.Category;
import core.category.model.CategoryType;
import core.cms.model.Block;
import core.product.model.Product;

public class CategoryController extends ActionSupport
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private CategoryManager cmanager ;
	private CmsManager cmsm ;
	private List<Category> categories ;
	private Category category;
	private Long uuid ;
	private String name ;
	private List<CategoryType> categoryTypes ;
	//default page size is 10 if not passed in request....
	private int pageSize ;
	private int p ;
	private int totalPage ;
	private String rt ;
	private Long cateType ;
	private Set<Product> products ;
	private Block block ;
	private String errorMsg ;
	private String successMsg ;
	private Long blockId ;
	private String content ;
	private Map<Long,String> path;
	private int confirmDelete ;
	private Long cateToDeleteId;
	
	public CategoryController()
	{
		cmanager = new CategoryManager();
		cmsm = new CmsManager();
	}
	public String execute() 
	{
		this.setCategory(cmanager.getCategoryByUuid(this.getUuid()));
		this.setProducts(this.getCategory().getProducts());
		CategoryPath cp = new CategoryPath(this.category.getPath());
		this.path = cp.getPathMap();
		if(this.getCategory().getCategoryType()!=null)
		{
			this.setCategoryTypes(cmanager.getValidCategoryType(this.getCategory()));
		}
		else
		{
			this.setCategoryTypes(cmanager.getAllCategoryType());
		}
		
		if(this.pageSize == 0)
		{
			this.setPageSize(10);
		}
		
		this.categories = cmanager.getCategoryList(this.getCategory());
		Paging<Category> paging = new Paging<Category>();
		this.setTotalPage(paging.getPages(this.categories.size(), this.pageSize));
		this.setCategories(paging.getPageList(this.pageSize, this.categories, this.p));
		return SUCCESS ;
	}
	public String createCategory()
	{
		String name = this.getName();
		this.category = cmanager.getCategoryByUuid(this.getUuid());
		Category parent = this.category ;
		Category c = new Category();
		c.setName(name);
		int depth = parent.getDepth() + 1 ;
		c.setDepth(depth);
		c.setParent(parent);
		c.setCategoryType(cmanager.getCategoryTypeByUUID(this.getCateType()));
		cmanager.save(c);
		String path=parent.getPath() + "/" + c.getUuid();
		c.setPath(path);
		cmanager.save(c);
		return this.execute()  ;
	}
	public String editCategory()
	{
		Category c  = cmanager.getCategoryByUuid(this.getUuid());
		if(c.getBlock() == null)
		{
			Block b = new Block();
			b.setIdentifier("categoryId_"+c.getUuid()+"_"+c.getName()+"_block");
			cmsm.addBlock(b);
			c.setBlock(b);
			cmanager.save(c);
		}
		this.setCategory(c);
		this.setBlock(c.getBlock());
		return SUCCESS ;
	}
	public String updateCategory()
	{
		Category c  = cmanager.getCategoryByUuid(this.getUuid());
		Block b = cmsm.getBlockByUuid(this.getBlockId());
		if(this.getName()==null || this.getName().trim().length() ==0)
		{
			this.setErrorMsg("Name can't be null");
			this.setCategory(c);
			this.setBlock(b);
			return ERROR ;
		}
		b.setContent(this.getContent());
		cmsm.addBlock(b);
		c.setBlock(b);
		c.setName(this.getName());
		cmanager.save(c);
		this.setCategory(c);
		this.setBlock(b);
		this.setSuccessMsg("Category successfully updated");
		return SUCCESS ;
	}
	public String deleteCategory()
	{
		Category toDelete =  cmanager.getCategoryByUuid(this.getCateToDeleteId());
		List<Category> toDeleteCates = cmanager.getSubCategoryByPath(toDelete.getPath());
		cmanager.massRemoveCategory(toDeleteCates);
		//cmanager
		return this.execute()  ;
	}
	public String confirmDelete()
	{
		this.setConfirmDelete(1);
		return this.execute()  ;
	}
	public Set<Product> getProducts() {
		return products;
	}
	public void setProducts(Set<Product> products) {
		this.products = products;
	}
	public Long getCateType() {
		return cateType;
	}
	public void setCateType(Long cateType) {
		this.cateType = cateType;
	}
	public String getRt() {
		return rt;
	}
	public void setRt(String rt) {
		this.rt = rt;
	}
	public List<CategoryType> getCategoryTypes() {
		return categoryTypes;
	}
	public void setCategoryTypes(List<CategoryType> categoryTypes) {
		this.categoryTypes = categoryTypes;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Long getUuid() {
		return uuid;
	}
	public void setUuid(Long uuid) {
		this.uuid = uuid;
	}
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public int getP() {
		return p;
	}
	public void setP(int p) {
		this.p = p;
	}
	
	public List<Category> getCategories()
	{
		return this.categories;
	}
	public void setCategories(List<Category> c)
	{
		this.categories = c;
	}
	public Block getBlock() {
		return block;
	}
	public void setBlock(Block block) {
		this.block = block;
	}
	public String getErrorMsg() {
		return errorMsg;
	}
	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}
	public String getSuccessMsg() {
		return successMsg;
	}
	public void setSuccessMsg(String successMsg) {
		this.successMsg = successMsg;
	}
	public Long getBlockId() {
		return blockId;
	}
	public void setBlockId(Long blockId) {
		this.blockId = blockId;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Map<Long, String> getPath() {
		return path;
	}
	public void setPath(Map<Long, String> path) {
		this.path = path;
	}
	public int getConfirmDelete() {
		return confirmDelete;
	}
	public void setConfirmDelete(int confirmDelete) {
		this.confirmDelete = confirmDelete;
	}
	public Long getCateToDeleteId() {
		return cateToDeleteId;
	}
	public void setCateToDeleteId(Long cateToDeleteId) {
		this.cateToDeleteId = cateToDeleteId;
	}
}
